package com.aaie.gru.getui.controller;

import com.aaie.gru.getui.PushtoSingle;
import com.aaie.gru.login.entity.LoginUser;
import com.aaie.gru.response.ResMessage;
import com.aaie.gru.user.entity.GruSysUser;
import com.aaie.gru.user.service.IGruSysUserService;
import com.aaie.gru.util.CommonUtil;
import com.aaie.gru.util.ExceptionEnums;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@Api(tags = "推送模块")
@RestController
@RequestMapping("/push")
public class PushController {

    @Autowired
    IGruSysUserService gruSysUserService;

    @ApiOperation(value = "监护人或员工给家长推送消息", notes = "监护人或员工给家长推送消息")
    @GetMapping("/push2Parent")
    public String push2Parent(String fatherId, String motherId, HttpSession session){
        try {
            LoginUser user = CommonUtil.getUser(session);
            if (user == null) {
                return ResMessage.error(ExceptionEnums.LOGINUSER_IS_NULL.getEmsg(), ExceptionEnums.LOGINUSER_IS_NULL.getEcode());
            }
            if (StringUtils.isEmpty(fatherId) && StringUtils.isEmpty(motherId)) {
                return ResMessage.error("未传入父亲和母亲信息", 200);
            }
            // 登录人的姓名
            String userName = user.getUserName();
            if (StringUtils.isNotEmpty(fatherId)) {
                GruSysUser gruSysUser = gruSysUserService.getById(fatherId);
                if (gruSysUser != null && StringUtils.isNotEmpty(gruSysUser.getUserClientId())) {
                    PushtoSingle.pushtoSingle("获取位置通知",userName+"想获取您的孩子的地理位置","{bizType:100}",gruSysUser.getUserClientId(),gruSysUser.getPlatform());
                }
            }
            if (StringUtils.isNotEmpty(motherId)) {
                GruSysUser gruSysUser = gruSysUserService.getById(fatherId);
                if (gruSysUser != null && StringUtils.isNotEmpty(gruSysUser.getUserClientId())) {
                    PushtoSingle.pushtoSingle("获取位置通知",userName+"想获取您的孩子的地理位置","{bizType:100}",gruSysUser.getUserClientId(),gruSysUser.getPlatform());
                }
            }
            return ResMessage.ok("给学生家长发送地理位置授权成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResMessage.error("给学生家长发送地理位置授权失败", 200);
        }
    }

    @ApiOperation(value = "家长给学生推送消息", notes = "家长给学生推送消息")
    @GetMapping("/push2Student")
    public String push2Student(HttpSession session){
        try {
            LoginUser user = CommonUtil.getUser(session);
            if (user == null) {
                return ResMessage.error(ExceptionEnums.LOGINUSER_IS_NULL.getEmsg(), ExceptionEnums.LOGINUSER_IS_NULL.getEcode());
            }
            // 登录人id
            String userId = user.getUserId();
            // 登录人的姓名
            String userName = user.getUserName();
            if (StringUtils.isNotEmpty(userId)) {
                QueryWrapper<GruSysUser> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("father_id", userId).or(qw -> qw.eq("mother_id", userId));
                List<GruSysUser> list = gruSysUserService.list(queryWrapper);
                if (!CollectionUtils.isEmpty(list)) {
                    for (GruSysUser gruSysUser : list) {
                        if (StringUtils.isNotEmpty(gruSysUser.getUserClientId())) {
                            PushtoSingle.pushtoSingle("获取位置通知", "您的家长" + userName + "同意获取您的地理位置，请打开应用", "{bizType:100}", gruSysUser.getUserClientId(),gruSysUser.getPlatform());
                        }
                    }
                }
            }
            return ResMessage.ok("同意获取您的地理位置授权成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResMessage.error("同意获取您的地理位置授权失败", 200);
        }
    }
}
